package com.yonyou.pmclouds.constructiondiary.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.constructiondiary.entity.ConstructionDiaryVO;
import com.yonyou.pmclouds.constructiondiary.rmiitf.ConstructionDiaryMaintain;
import com.yonyou.pmclouds.constructiondiary.rmiitf.ConstructionDiaryQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 施工日志
 */
@Slf4j
@RequestMapping("/mobile/constructiondiary")
@RestController
public class ConstructionDiaryController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ConstructionDiaryMaintain maintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ConstructionDiaryQuery diaryQuery;
    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;

    private static final String[] USER_ID_FIELDS = {"creator"};
    private static final String[] USER_NAME_FIELDS = {"creatorName"};

    /**
     * 提交
     */
    @Insert
    @RequestMapping(value = "/add")
    @PMApprove
    public ConstructionDiaryVO add(@RequestBody ConstructionDiaryVO constructionDiaryVO) throws BusinessException {
        if (constructionDiaryVO == null) return null;
        return maintain.insert(constructionDiaryVO);
    }

    /**
     * 获取日志列表
     */
    @RequestMapping(value = "/getList")
    public AdditionalPropertiesVOWrapper[] getList(@RequestParam("pkProject") String pkProject) throws BusinessException {
        if (StringUtils.isEmpty(pkProject)) {
            return null;
        }
        ConstructionDiaryVO[] result = diaryQuery.queryDiaryListInfo(pkProject);
        userQuery.tranUserInfoField(result, USER_ID_FIELDS, USER_NAME_FIELDS);
        result = result == null ? new ConstructionDiaryVO[0] : result;
        return propertiesProcessor.wrapAdditionalProps(result);
    }

    /**
     * 获取日志详情
     */
    @RequestMapping(value = "/getDetail")
    public ConstructionDiaryVO getDetail(@RequestParam("pkConstructiondiary") String pkConstructiondiary) throws BusinessException {
        if (pkConstructiondiary == null) return null;
        ConstructionDiaryVO result = diaryQuery.queryDetailByID(pkConstructiondiary);
        if (result != null) {
            userQuery.tranUserInfoField(new ConstructionDiaryVO[]{result}, USER_ID_FIELDS, USER_NAME_FIELDS);
        }
        return result;
    }


}
